package mydictionaryry;

import java.util.HashMap;
import java.util.Iterator;

/**
 * @author Administrator
 * 工具实现类:集合实现
 */
public class DictionaryDaoMap implements DictionaryDao{
  private HashMap<String,MyDictionary>  hashMap=new HashMap<String,MyDictionary>();
  @Override
  public void save(MyDictionary myDictionary) {
    String key=myDictionary.getEnglish();
    hashMap.put(key,myDictionary);
  }

  @Override
  public MyDictionary query(String name) {
    if(hashMap.containsKey(name)){
      return hashMap.get(name) ;
    }
    return null;
  }

  @Override
  public void queryAll() {
    System.out.println("词典中所有内容如下：");
    Iterator iterator= hashMap.keySet().iterator();
    while (iterator.hasNext()){
      String key= (String) iterator.next();
      MyDictionary value=hashMap.get(key);
      System.out.println(value);
    }
  }

  @Override
  public void delete(String name) {
    if(hashMap.containsKey(name)){
      hashMap.remove(name);
      System.out.println("该单词已经删除");
    }else{
      System.out.println("该单词不存在");
    }
  }
}
